<?php
/*
Plugin Name: WPU-Chunk Widget
Plugin URI: http://tacoen.smedia.or.id
Description: Widgetizing WPU-chunks. <a href="http://svn.wp-plugins.org/widgets/trunk">Automattic's Sidebar Widget</a> is needed for this plugins. <br/><small>This plugins is automatic activated when you selecting WPU themes at the first time.</small>
Version: 0.3
Author: tacoen
Author URI: http://tacoen.smedia.or.id
*/

function widget_wpu_php_init()
{
  if (!function_exists('register_sidebar_widget'))
    return;
  
  function widget_wpu_php($args, $number= 1)
  {
    extract($args);
    
    $options = get_option('widget_wpu_php');
    $title = $options[$number]['title'];
    $text = $options[$number]['text'];

    echo $before_widget;
    
    
    if ($title)
    {
      echo $before_title;
      echo $title;
      echo $after_title;
    }

    $dotext = "<?php ta_include('".$text."') ?>";
    echo eval('?>' . $dotext);

  }

function wpu_chunk_browse($selected,$number) {
	if (file_exists(ta_incpath())) {
		echo "<select name='wpu-php-text-".$number."'> id='wpu-php-text-".$number."'>";
		if ($handle = opendir(ta_incpath())) {
			while (false !== ($file = readdir($handle))) {
				if ( substr($file, strrpos ($file, '.')) == ".php") { 
					$filelist[$countfile] = $file; $countfile++; 
				}					
	   		}
		}
		closedir($handle);

	   	sort($filelist,SORT_STRING); foreach($filelist as $file) {
			if ($selected == $file) { $sel = " selected='selected'"; }
			if (preg_match ("/widget-/i",$file)) {
				echo "\t<option value='$file'".$sel.">$file</option>/n"; $sel="";
			}
		}
		echo "</select>\n";
	}
}

  
  function widget_wpu_php_control($number)
  {
    $options = get_option('widget_wpu_php');
    if ($_POST["wpu-php-submit-$number"])
    {
      $options[$number]['title'] = stripslashes($_POST["wpu-php-title-$number"]);
      $options[$number]['text'] = stripslashes($_POST["wpu-php-text-$number"]);
      update_option('widget_wpu_php', $options);
    }
    
	$title = htmlspecialchars($options[$number]['title'], ENT_QUOTES);
	$text = htmlspecialchars($options[$number]['text'], ENT_QUOTES);    
?>
    <p>Title <input style="width: 250px;" id="wpu-php-title-<?php echo "$number"; ?>" name="wpu-php-title-<?php echo "$number"; ?>" value="<?php echo $title; ?>" /></p>
    <p>Chunk file to include : <?php wpu_chunk_browse($text,$number); ?><br/><small>Note: Some files could not be included.</small></p>
    <input type="hidden" id="wpu-php-submit-<?php echo "$number"; ?>" name="wpu-php-submit-<?php echo "$number"; ?>" value="1" />

<?php
  }

  function widget_wpu_php_setup() 
  {
    $options = get_option('widget_wpu_php');
    if ($_POST['wpu-php-number-submit']) 
    {
      $number = (int)$_POST['wpu-php-number'];
      if ( $number > 9 ) $number = 9;
      if ( $number < 1 ) $number = 1;
      $options['number'] = $number;
      update_option('widget_wpu_php', $options);
      widget_wpu_php_register($options['number']);
    }
  }  

function widget_wpu_php_page() 
  {
    $options = get_option('widget_wpu_php');
  ?>
    <div class="wrap">
      <form method="post">
        <h2><?php _e('WPU Chunk Widgets', 'widgets'); ?></h2>
        <p style="line-height: 30px;"><?php _e('How many WPU Chunk widgets would you like?', 'widgets'); ?>
        <select id="wpu-php-number" name="wpu-php-number" value="<?php echo $options['number']; ?>">
  <?php for ( $i = 1; $i < 10; ++$i ) echo "<option value='$i' ".($options['number']==$i ? "selected='selected'" : '').">$i</option>"; ?>
        </select>
        <span class="submit"><input type="submit" name="wpu-php-number-submit" id="wpu-php-number-submit" value="<?php _e('Save'); ?>" /></span></p>
      </form>
    </div>
  <?php
  }

  function widget_wpu_php_register() 
  {
    $options = get_option('widget_wpu_php');
    $number = $options['number'];
    if ( $number < 1 ) $number = 1;
    if ( $number > 9 ) $number = 9;
    for ($i = 1; $i <= 9; $i++) {
      $name = array('WPU CHUNK %s', 'widgets', $i);
      register_sidebar_widget($name, $i <= $number ? 'widget_wpu_php' : /* unregister */ '', $i);
      register_widget_control($name, $i <= $number ? 'widget_wpu_php_control' : /* unregister */ '', 460, 118, $i);
    }
    add_action('sidebar_admin_setup', 'widget_wpu_php_setup');
    add_action('sidebar_admin_page', 'widget_wpu_php_page');
  }  
  
  widget_wpu_php_register();
}

add_action('plugins_loaded', 'widget_wpu_php_init');

?>
